/**
 * Copyright (c) 2020 QingLang, Inc. <baisui@qlangtech.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.qlangtech.tis.manage.spring.aop;

import java.util.regex.Pattern;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
import org.springframework.beans.BeansException;
import com.qlangtech.tis.manage.biz.dal.dao.IDepartmentDAO;

/**
 * @author 百岁（baisui@qlangtech.com）
 * @date 2012-5-29
 */
public class DAOTypeAutoProxyCreator extends AbstractAutoProxyCreator {

    private static final long serialVersionUID = 1L;

    private static final Pattern methodPattern = Pattern.compile("^([^((get)(count)(select))]).*");

    public static void main(String[] arg) {
        // System.out.println(PatternMatchUtils.simpleMatch("updateUser",
        // ));
        System.out.println(methodPattern.matcher("updateUser").matches());
        System.out.println(methodPattern.matcher("getUser").matches());
        System.out.println(methodPattern.matcher("countUser").matches());
        System.out.println(methodPattern.matcher("selectUser").matches());
    }

    @Override
    protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource customTargetSource) throws BeansException {
        if (beanClass == null || beanClass.getPackage() == null) {
            return DO_NOT_PROXY;
        }
        try {
            if (beanClass.equals(com.qlangtech.tis.manage.biz.dal.dao.impl.OperationLogDAOImpl.class) || IDepartmentDAO.class.isAssignableFrom(beanClass)) {
                return DO_NOT_PROXY;
            }
        } catch (Exception e) {
            throw new BeansException(e.getMessage(), e) {

                private static final long serialVersionUID = 1L;
            };
        }
        return ((com.qlangtech.tis.manage.common.OperationLogger.class.isAssignableFrom(beanClass))) ? PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS : DO_NOT_PROXY;
    }
}
